package com.lenovo.rc.sil.wangdwa.learning;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class IntentDataTestActivity extends Activity implements OnClickListener{
	
    public static final String EXTRA_KEY = "EXTRA_KEY";
    public static final int REQ_CODE = 2012;
    
    private ArrayList<Kid> kids = new ArrayList<Kid>();

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button)findViewById(R.id.BTN_START);
        btn.setOnClickListener(this);
        
        Log.d(this.getClass().getName(), "onCreate()");
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.BTN_START:
			doStart();
			break;
		}
	}

	private void doStart() {
		// TODO Auto-generated method stub
		Log.d(this.getClass().getName(), "doStart()");
		
		Intent startNew = new Intent(this, DataViewerActivity.class);
		ArrayList<Kid> kids = initArrayList();
		startNew.putParcelableArrayListExtra(EXTRA_KEY, kids);
//		this.startActivity(startNew);
		
		this.startActivityForResult(startNew, REQ_CODE);
	}

	private ArrayList<Kid> initArrayList() {
		// TODO Auto-generated method stub
		addKid(kids, "Davy", Kid.SEX_MALE, "2012-01-02");
		addKid(kids, "Vickey", Kid.SEX_FEMALE, "2012-03-04");
		
		return kids;
	}

	private void addKid(ArrayList<Kid> kids, String name, int sex, String birthday) {
		// TODO Auto-generated method stub
		kids.add(new Kid(name, sex, birthday));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == REQ_CODE) {
			Bundle b = data.getExtras();
			String msg = b.getString("msg");
			Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
			

			
		}
		
	}
    
    
}